import fs from 'fs/promises'
import fse from 'fs-extra'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))
export const rootDir = resolve(__dirname)
export const projectConfig = readProjectConfig()
export const cacheDir = resolve(rootDir, 'cache')
export const dataDir = resolve(rootDir, 'data')

fs.mkdir(cacheDir, { recursive: true })
fs.mkdir(dataDir, { recursive: true })

function readProjectConfig() {
  // const json = await fse.readJsonSync('./project-config.json', { throws: false })
  const json = fse.readJsonSync('./project-config.json', { throws: false })

  return {
    contentIndex: 0,
    ...json,
  }
}

export const saveProjectConfig = () => {
  fse.writeJSONSync('./project-config.json', projectConfig)
}
